﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;
using System.IO;
using dotNails.Util;
using System.Diagnostics;
using Microsoft.Win32;
using dotNails.CodeGen.Util;


namespace dotNails {
	[RunInstaller(true)]
	public partial class dotNailsInstaller : Installer {
		public dotNailsInstaller() {
			InitializeComponent();
		}

		public override void Install(System.Collections.IDictionary stateSaver) {
			base.Install(stateSaver);
			
		}

		public override void Rollback(System.Collections.IDictionary savedState) {
			try {
				base.Rollback(savedState);

				//Remove the files or directory created and environment path if any set by setup
				string installationFilePath = base.Context.Parameters["AssemblyPath"];
				string installationFolderPath = installationFilePath.Substring(0, installationFilePath.LastIndexOf("\\"));

				if (Directory.Exists(installationFolderPath)) {
					Directory.Delete(installationFolderPath, true);
				}
			}
			catch (Exception ex) {
				LogUtil.EventLogMessage("Exception in Rollback: " + ex.Message);
			}
		}

		public override void Uninstall(System.Collections.IDictionary savedState) {
			//string target_dir = this.Context.Parameters["TargetDir"];

			base.Uninstall(savedState);

			try {

				string installationPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\dotNails", "InstallationPath", string.Empty).ToString();
				string valueName = Path.Combine(installationPath, "AddIns");
				RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\VisualStudio\9.0\AutomationOptions\LookInFolders", true);
				key.DeleteValue(valueName);
			}
			catch (Exception ex) {
				LogUtil.EventLogMessage("Exception in Uninstall: " + ex.Message);
			}
			//Remove the files or directory created and environment path if any set by setup
			//string installationPath = base.Context.Parameters["AssemblyPath"];
			//if (Directory.Exists(installationPath)) {
			//    Directory.Delete(installationPath, true);
			//}

		}
	}

	//public class LogUtil {
	//    public static void EventLogMessage(string message) {
	//        EventLog eventLog = new EventLog();
	//        eventLog.Source = "dotNails";
	//        eventLog.WriteEntry(message);
	//    }
		
	//    public static void EventLogMessage(string message, string innerException, string stackTrace) {
	//        EventLog eventLog = new EventLog();
	//        eventLog.Source = "dotNails";
	//        eventLog.WriteEntry(message + "\nInnerException:\n" + innerException + "\nStack Trace:\n" + stackTrace);
	//    }
	//}
}
